ig.module( 
	'game.main' 
)
.requires(
    'impact.game',
    'game.levels.dorm1',
    'game.levels.dorm2',
    'impact.font'
)

.defines(function(){

    ig.Game.inject({
        // Default init function for all game classes
        init: function () {

            // Reset all input bindings
            ig.input.unbindAll();
            
            // Add event listner to make sure we capture resize events
            window.addEventListener("resize", this.onViewStateChanged);

        },
        onViewStateChanged: function (eventArgs) {

            var viewStates = Windows.UI.ViewManagement.ApplicationViewState;
            var newViewState = Windows.UI.ViewManagement.ApplicationView.value;

            var scale = 5;
            var maxWidth = (window.innerWidth > 1366) ? 1366 : window.innerWidth;
            var maxHeight = (window.innerHeight > 786) ? 786 : window.innerHeight;
            var width = Math.floor(maxWidth / scale) - 1;
            var height = Math.floor(maxHeight / scale) - 1;

            var controls = document.getElementById("controls");

            ig.system.resize(width, height, scale);

            if (newViewState === viewStates.snapped) {
                // is snapped
                document.getElementById('controls').style.visibility = "hidden";
            } else if (newViewState === viewStates.filled) {
                // is filled
                if (window.navigator.msPointerEnabled)
                    document.getElementById('controls').style.visibility = "visible";
            } else if (newViewState === viewStates.fullScreenLandscape) {
                // is full screen
                if (window.navigator.msPointerEnabled)
                    document.getElementById('controls').style.visibility = "visible";
            }

        }
    })

MyGame = ig.Game.extend({
    gravity: 300,
    instructText: new ig.Font( 'media/04b03.font.png' ),
    statText: new ig.Font( 'media/04b03.font.png' ),
    showStats: false,
    statMatte: new ig.Image('media/stat-matte.png'),
    levelTimer: new ig.Timer(),
    levelExit: null,
    stats: {time: 0, kills: 0, deaths: 0},
    lives: 3,
    lifeSprite: new ig.Image('media/life-sprite.png'),
    init: function () {
        this.parent();
        this.loadLevel( LevelDorm1 );
        // Bind keys
        ig.input.bind( ig.KEY.LEFT_ARROW, 'left' );
        ig.input.bind( ig.KEY.RIGHT_ARROW, 'right' );
        ig.input.bind( ig.KEY.X, 'jump' );
        ig.input.bind( ig.KEY.C, 'shoot' );
        ig.input.bind( ig.KEY.TAB, 'switch' );
        ig.input.bind(ig.KEY.SPACE, 'continue');

	    // Touch Controls
        if (window.navigator.msPointerEnabled) {
            ig.input.bindTouch("#buttonLeft", "left");
            ig.input.bindTouch("#buttonRight", "right");
            ig.input.bindTouch("#buttonJump", "jump");
            ig.input.bindTouch("#buttonShoot", "shoot");
            ig.input.bindTouch("#canvas", "continue");
            ig.music.add('media/sounds/theme.*');
        }
        ig.music.volume = 0.5;
        ig.music.play();
        if (window.navigator.msPointerEnabled)
            document.getElementById('controls').style.visibility = "visible";
	},
    loadLevel: function( data ) {
        this.stats = {time: 0, kills: 0, deaths: 0};
    	this.parent(data);
        this.levelTimer.reset();
    },
    update: function() {
    	// screen follows the player
    	var player = this.getEntitiesByType( EntityPlayer )[0];
    	if( player ) {
    		this.screen.x = player.pos.x - ig.system.width/2;
    		this.screen.y = player.pos.y - ig.system.height/2;
            if(player.accel.x > 0 && this.instructText)
                this.instructText = null;
    	}
    	// Update all entities and BackgroundMaps
        if(!this.showStats){
        	this.parent();
        }else{
            if(ig.input.state('continue')){
                this.showStats = false;
                this.levelExit.nextLevel();
                this.parent();
            }
        }
    },
	draw: function() {
		// Draw all entities and backgroundMaps
		this.parent();
        if(this.instructText){
            var x = ig.system.width/2,
            y = ig.system.height - 10;
            this.instructText.draw( 'Left/Right Moves, X Jumps, C Fires & Tab Switches Weapons.', x, y, ig.Font.ALIGN.CENTER );
        }
        if(this.showStats){
            this.statMatte.draw(0,0);
            var x = ig.system.width/2;
            var y = ig.system.height/2 - 20;
            this. statText.draw('Level Complete', x, y, ig.Font.ALIGN.CENTER);
            this. statText.draw('Time: '+this.stats.time, x, y+30, ig.Font.ALIGN.CENTER);
            this. statText.draw('Kills: '+this.stats.kills, x, y+40, ig.Font.ALIGN.CENTER);
            this. statText.draw('Deaths: '+this.stats.deaths, x, y+50, ig.Font.ALIGN.CENTER);
            this. statText.draw('Press Spacebar to continue.', x, ig.system.height - 10, ig.Font.ALIGN.CENTER);
        }
        this.statText.draw("Lives", 5,5);
        for(var i=0; i < this.lives; i++)
            this.lifeSprite.draw(((this.lifeSprite.width + 2) * i)+5, 15);
	},
    toggleStats: function(levelExit){
        this.showStats = true;
        this.stats.time = Math.round(this.levelTimer.delta());
        this.levelExit = levelExit;
    },
    gameOver: function(){
        ig.finalStats = ig.game.stats;
        ig.system.setGame(GameOverScreen);
    }
});

StartScreen = ig.Game.extend({
    instructText: new ig.Font( 'media/04b03.font.png' ),
    background: new ig.Image('media/screen-bg.png'),
    mainCharacter: new ig.Image('media/screen-main-character.png'),
    title: new ig.Image('media/game-title.png'),
    init: function () {
        this.parent();
        ig.input.bind(ig.KEY.SPACE, 'start');
        if (window.navigator.msPointerEnabled) {
            ig.input.bindTouch("#canvas", "start");
        }
        document.getElementById('controls').style.visibility = "hidden";
    },
    update: function() {
        if(ig.input.pressed ('start')){
            ig.system.setGame(MyGame)
        }
        this.parent();
    },
    draw: function() {
        this.parent();
        this.background.draw(0,0);
        this.mainCharacter.draw(0,0);
        this.title.draw(ig.system.width - this.title.width, 0);
        var x = ig.system.width/2,
        y = ig.system.height - 10;
        this.instructText.draw( 'Press Spacebar To Start', x+40, y, ig.Font.ALIGN.CENTER );
    }
});

GameOverScreen = ig.Game.extend({
    instructText: new ig.Font( 'media/04b03.font.png' ),
    background: new ig.Image('media/screen-bg.png'),
    gameOver: new ig.Image('media/game-over.png'),
    stats: {},
    init: function () {
        this.parent();
        ig.input.bind(ig.KEY.SPACE, 'start');
        if (window.navigator.msPointerEnabled) {
            ig.input.bindTouch("#canvas", "start");
        }
        this.stats = ig.finalStats;
        document.getElementById('controls').style.visibility = "hidden";
    },
    update: function() {
        if(ig.input.pressed('start')){
            ig.system.setGame(StartScreen)
        }
        this.parent();
    },
    draw: function() {
        this.parent();
        this.background.draw(0,0);
        var x = ig.system.width/2;
        var y = ig.system.height/2 - 20;
        this.gameOver.draw(x - (this.gameOver.width * .5), y - 30);
        var score = (this.stats.kills * 100) - (this.stats.deaths * 50);
        this.instructText.draw('Total Kills: '+this.stats.kills, x, y+30, ig.Font.ALIGN.CENTER);
        this.instructText.draw('Total Deaths: '+this.stats.deaths, x, y+40, ig.Font.ALIGN.CENTER);
        this.instructText.draw('Score: '+score, x, y+50, ig.Font.ALIGN.CENTER);
        this.instructText.draw('Press Spacebar To Continue.', x, ig.system.height - 10, ig.Font.ALIGN.CENTER);
    }
});

ig.Input.inject({
    bindTouch: function (selector, action) {
        var element = ig.$(selector);

        var that = this;
        element.addEventListener('MSPointerDown', function (ev) {
            that.touchStart(ev, action);
        }, false);

        element.addEventListener('MSPointerUp', function (ev) {
            that.touchEnd(ev, action);
        }, false);
    },
})

// Start the Game with 60fps, a resolution of 320x240, scaled
// up by a factor of 2
var scale = 5;
var maxWidth = (window.innerWidth > 1366) ? 1366 : window.innerWidth;
var maxHeight = (window.innerHeight > 786) ? 786 : window.innerHeight;
var width = Math.floor(maxWidth / scale) - 1;
var height = Math.floor(maxHeight / scale) - 1;

ig.startNewGame = function () { ig.main('#canvas', StartScreen, 60, width, height, scale, ig.ImpactSplashLoader); };

});
